<template>
  <div class="service-form-container" style="display: flex; flex-direction: column; height: 100%;">
    <!-- 服务需求统计表单 -->
    <el-card class="custom-card" style="flex: 1; margin-bottom: 15px; display: flex; flex-direction: column;">
      <div slot="header" class="card-header">
        <span style="color: #fff">服务需求统计</span>
      </div>

      <!-- 数据显示区域，使用v-loading指令 -->
      <div
        class="service-form"
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        
      >
        <!-- 错误状态 -->
        <div v-if="error" class="error-message">
          <i class="el-icon-error"></i>
          <span>{{ error }}</span>
          <el-button type="primary" size="small" @click="fetchServiceData"
            >重试</el-button
          >
        </div>

        <!-- 数据显示（仅在没有错误时显示） -->
        <div v-if="!error">
          <div class="form-row">
            <div class="form-item">
              <div class="stat-number orange">{{ 种植需求 }}</div>
              <div class="stat-name">种植</div>
            </div>
            <div class="form-item">
              <div class="stat-number blue">{{ 农资需求 }}</div>
              <div class="stat-name">农资</div>
            </div>
            <div class="form-item">
              <div class="stat-number orange">{{ 耕种需求 }}</div>
              <div class="stat-name">耕种</div>
            </div>
          </div>
          <div class="form-row">
            <div class="form-item">
              <div class="stat-number blue">{{ 飞防需求 }}</div>
              <div class="stat-name">飞防</div>
            </div>
            <div class="form-item">
              <div class="stat-number orange">{{ 收割需求 }}</div>
              <div class="stat-name">收割</div>
            </div>
            <div class="form-item">
              <div class="stat-number blue">{{ 储藏需求 }}</div>
              <div class="stat-name">储藏</div>
            </div>
          </div>
        </div>
      </div>
    </el-card>

    <!-- 各产业订单数量 -->
    <el-card class="custom-card" style="flex: 1;">
      <div slot="header" class="card-header">
        <span style="color: #fff">各产业订单数量</span>
      </div>
      <div
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        style="width: 100%;"
      >
        <div id="orderChart" style="width: 100%; flex: 1; min-height: 398px"></div>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from "echarts";
import axios from "axios";

const baseApi = "http://localhost:15000";

export default {
  name: "ServiceForm",
  data() {
    return {
      种植需求: 0,
      农资需求: 0,
      耕种需求: 0,
      飞防需求: 0,
      收割需求: 0,
      储藏需求: 0,
      orderData: [],
      orderCategories: [],
      loading: false,
      error: "",
    };
  },
  created() {
    // 组件创建时调用API获取数据
    this.fetchServiceData();
    this.fetchOrderData();
  },
  mounted() {
    // 确保在数据加载后初始化图表
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    // 调用接口获取服务数据
    fetchServiceData() {
      this.loading = true;
      this.error = "";

      // 调用实际API获取服务需求数据
      axios
        .get(baseApi + "/service/statistics/demand")
        .then((response) => {
          console.log("服务数据获取成功：", response.data);
          if (response.data.code === 200) {
            // 更新组件数据
            this.种植需求 = response.data.data.种苗 || 0;
            this.农资需求 = response.data.data.农资 || 0;
            this.耕种需求 = response.data.data.耕种 || 0;
            this.飞防需求 = response.data.data.飞防 || 0;
            this.收割需求 = response.data.data.收割 || 0;
            this.储藏需求 = response.data.data.储藏 || 0;
            this.error = "";
            console.log("服务数据更新成功：", response.data.data);

            // 数据更新后重新初始化图表
            this.$nextTick(() => {
              this.initChart();
            });
          } else {
            this.error =
              "获取服务数据失败：" + (response.data.message || "未知错误");
          }
          this.loading = false;
        })
        .catch((error) => {
          this.error = "网络请求失败，请检查网络连接";
          this.loading = false;
          console.error("获取服务数据失败:", error);
        });
    },

    // 获取订单数量数据
    fetchOrderData() {
      this.loading = true;

      // 调用实际API获取订单数量数据
      axios
        .get(baseApi + "/service/statistics/orderNumber")
        .then((response) => {
          console.log("订单数据获取成功：", response.data);

          if (response.data.code === 200) {
            // 解析实际返回的数据结构
            const data = response.data.data;

            // 提取订单数据和分类
            this.orderData = [
              data.果树茶叶 || 0,
              data.渔业养殖 || 0,
              data.畜牧养殖 || 0,
              data.粮棉油糖 || 0,
              data.蔬菜花卉 || 0,
            ];

            this.orderCategories = [
              "果树茶叶",
              "渔业养殖",
              "畜牧养殖",
              "粮棉油糖",
              "蔬菜花卉",
            ];

            console.log("解析后的订单数据:", this.orderData);
            console.log("解析后的分类:", this.orderCategories);

            // 数据更新后重新初始化图表
            this.$nextTick(() => {
              this.initChart();
            });
          } else {
            console.warn(
              "获取订单数据失败：",
              response.data.message || "未知错误"
            );
            // 保持默认数据
            this.orderData = [0, 0, 0, 0, 0];
            this.orderCategories = [
              "果树茶叶",
              "渔业养殖",
              "畜牧养殖",
              "粮棉油糖",
              "蔬菜花卉",
            ];
          }
          this.loading = false;
        })
        .catch((error) => {
          console.error("获取订单数据网络请求失败:", error);
          // 保持默认数据
          this.orderData = [0, 0, 0, 0, 0];
          this.orderCategories = [
            "果树茶叶",
            "渔业养殖",
            "畜牧养殖",
            "粮棉油糖",
            "蔬菜花卉",
          ];
          this.loading = false;
        });
    },

    initChart() {
      // 订单数量图表
      const orderChart = echarts.init(document.getElementById("orderChart"));
      const orderOption = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: this.orderCategories,
          axisLine: {
            lineStyle: {
              color: "#8799a3",
            },
          },
          axisLabel: {
            color: "#8799a3",
          },
        },
        yAxis: {
          type: "value",
          axisLine: {
            lineStyle: {
              color: "#8799a3",
            },
          },
          axisLabel: {
            color: "#8799a3",
          },
          splitLine: {
            lineStyle: {
              color: "rgba(135, 153, 163, 0.2)",
            },
          },
        },
        series: [
          {
            data: this.orderData,
            type: "bar",
            itemStyle: {
              color: function () {
                const colorList = ["#f39c12", "#3498db", "#e74c3c", "#2ecc71"];
                return colorList[Math.floor(Math.random() * colorList.length)];
              },
            },
          },
        ],
        backgroundColor: "transparent",
      };
      orderChart.setOption(orderOption);

      // 窗口大小改变时，重新调整图表大小
      window.addEventListener("resize", () => {
        if (orderChart && typeof orderChart.resize === "function") {
          orderChart.resize();
        }
      });
    },
  },
};
</script>

<style scoped>
/* 基础样式重置 */
* {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

h1,
h2,
h3,
h4,
div {
  padding: 0;
  margin: 0;
  font-weight: normal;
}

/* 服务表单容器样式 */
.service-form-container {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* 容器背景装饰 - 添加重复的背景图案，增强视觉效果 */
.service-form-container::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url("../assets/dec/bg.png") repeat;
  opacity: 0.1;
  z-index: -2;
}

/* 服务需求统计表单卡片样式 - 为第一张卡片添加背景图片和透明度 */
.el-card.custom-card:first-child {
  position: relative;
  background: url("../assets/dec/bor1_1.png") no-repeat top;
  background-size: 100% 100%;
  opacity: 0.9;
  z-index: 1;
  border: 0 solid transparent;
}

/* 卡片下层装饰 - 为第一张卡片添加底层装饰图案 */
.el-card.custom-card:first-child::before {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("../assets/dec/bor1_2.png") no-repeat;
  background-size: 100% 100%;
  z-index: -1;
  content: "";
}

/* 产业订单数量卡片样式 - 为第二张卡片设置背景色和边框样式 */
.el-card.custom-card:last-child {
  position: relative;
  background: rgba(18, 22, 64, 0.5);
  margin-top: 10px;
  /* border-top: 4px solid #121e52; */
  /* border-bottom: 4px solid #10144b; */
  opacity: 0.9;
  border: 0 solid transparent;
}

/* 第二张卡片背景装饰 - 为订单数量卡片添加背景图案 */
.el-card.custom-card:last-child::before {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("../assets/dec/bor1_2.png") no-repeat;
  background-size: 100% 100%;
  z-index: -1;
  content: "";
}

/* 第二张卡片右上角装饰 - 为订单数量卡片添加角标装饰 */
.el-card.custom-card:last-child::after {
  content: "";
  position: absolute;
  top: 5.6%;
  right: -2%;
  z-index: 100;
  width: 15%;
  height: 7%;
  background: url("../assets/dec/bor1_2_square.png") no-repeat;
  background-size: 100%;
  z-index: 10;
}

/* 卡片标题样式 - 设置标题的位置、字体大小和颜色 */
.card-header {
  position: relative;
  font-size: 0.5625em;
  color: #fff;
  padding-left: 13%;
}

/* 标题图标 - 在标题左侧添加装饰图标 */
.card-header::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  width: 8%;
  height: 60%;
  background: url("../assets/dec/bor2_3.png") no-repeat center;
  background-size: contain;
}

/* 表单区域样式 - 设置表单的内边距和高度 */
.service-form {
  padding: 15px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* 表单行布局 - 使用flex布局设置表单行的对齐方式 */
.form-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}

/* 表单项样式 - 设置每个统计项的外观和位置 */
.form-item {
  flex: 1;
  background: rgba(8, 29, 93, 0.8);
  border-radius: 8px;
  padding: 15px 10px;
  text-align: center;
  position: relative;
  overflow: hidden;
  border: 1px solid #1e4d85;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease;
  margin: 0 5px;
}

/* 表单项顶部装饰线 - 添加顶部发光效果 */
.form-item:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00bbec, transparent);
}

/* 表单项底部装饰线 - 添加底部渐变效果 */
.form-item:after {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, #1e4d85, transparent);
}

/* 表单项悬停效果 - 鼠标悬停时提升卡片并增强阴影 */
.form-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 187, 236, 0.3);
  border-color: #00bbec;
}

/* 统计数字样式 - 设置数字的字体、大小和效果 */
.stat-number {
  font-family: DigifaceWide;
  font-size: 1.25em;
  font-weight: bold;
  margin-bottom: 8px;
  text-shadow: 0 0 10px rgba(0, 187, 236, 0.5);
  letter-spacing: 1px;
}

/* 橙色数字样式 - 为特定统计项设置橙色 */
.stat-number.orange {
  color: #ff9500;
}

/* 蓝色数字样式 - 为特定统计项设置蓝色 */
.stat-number.blue {
  color: #00bbec;
}

/* 统计名称样式 - 设置统计项名称的颜色和字体大小 */
.stat-name {
  color: #8aa4ce;
  font-size: 0.4375em;
  letter-spacing: 0.5px;
}

/* 单位样式 - 为统计数字添加"万"单位 */
.stat-number.unit-wan:after {
  display: inline-block;
  margin-left: 4%;
  font-size: 12px;
  color: #fff;
  content: "万";
}

/* 订单图表容器 - 设置图表的位置属性以便添加背景 */
#orderChart {
  position: relative;
}

/* 图表背景装饰 - 为图表添加半透明的背景图案 */
#orderChart::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url("../assets/dec/bor1_3.png") no-repeat center center;
  background-size: contain;
  opacity: 0.1;
  pointer-events: none;
  z-index: -1;
}

/* 装饰性元素 - 为表单项添加顶部渐变效果 */
.form-item::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00bbec, transparent);
}

/* 装饰性元素 - 为表单项添加底部渐变效果 */
.form-item::after {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, #1e4d85, transparent);
}

/* 表单背景装饰 - 为service-form添加圆形装饰背景 */
.service-form {
  position: relative;
  z-index: 1;
}

.service-form::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  height: 80%;
  background: url("../assets/dec/bg-circle.png") no-repeat center;
  background-size: contain;
  opacity: 0.1;
  z-index: -1;
}

/* 响应式调整 - 针对大屏幕的字体大小调整 */
@media screen and (max-width: 1930px) {
  .stat-number {
    font-size: 1em;
  }

  .stat-name {
    font-size: 0.375em;
  }
}

/* 标题文本样式 - 设置卡片标题的字体大小和粗细 */
.card-header span {
  position: relative;
  z-index: 1;
  font-size: 18px;
  font-weight: bold;
}

/* 标题下划线效果 - 为标题添加底部渐变线条装饰 */
.card-header span::before {
  content: "";
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, #00bbec, transparent);
  z-index: -1;
}

/* 表单项交替颜色 - 为不同位置的表单项设置不同的顶部渐变颜色 */
.form-item:nth-child(1)::before {
  background: linear-gradient(90deg, transparent, #ff9500, transparent);
}

.form-item:nth-child(2)::before {
  background: linear-gradient(90deg, transparent, #00bbec, transparent);
}

.form-item:nth-child(3)::before {
  background: linear-gradient(90deg, transparent, #ff9500, transparent);
}

/* 错误消息样式 - 网络加载失败时保持原样 */
.error-message {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 200px;
  color: #f56c6c;
  background-color: rgba(245, 108, 108, 0.05);
  border: 1px solid rgba(245, 108, 108, 0.2);
  border-radius: 4px;
  padding: 20px;
  text-align: center;
}
</style>
